package at.fhj.itm10.ticketmaster.services.impl;
/**
 * EventTypeServiceImpl
 * 
 * @author Gerald Hambrosch, Benjamin Hirt
 */
import java.util.List;

import org.apache.log4j.Logger;

import at.fhj.itm10.ticketmaster.dao.DAOException;
import at.fhj.itm10.ticketmaster.dao.EventTypeDAO;
import at.fhj.itm10.ticketmaster.entities.Event;
import at.fhj.itm10.ticketmaster.entities.EventType;
import at.fhj.itm10.ticketmaster.helper.Validator;
import at.fhj.itm10.ticketmaster.services.EventTypeService;
import at.fhj.itm10.ticketmaster.services.ServiceException;

public class EventTypeServiceImpl extends AbstractService implements EventTypeService {
	
	private Logger logger = Logger.getLogger(EventTypeServiceImpl.class);
	private EventTypeDAO eventTypeDao;

	public EventTypeDAO getEventTypeDao() {
		return eventTypeDao;
	}

	public void setEventTypeDao(EventTypeDAO eventTypeDao) {
		this.eventTypeDao = eventTypeDao;
	}

	/**
	 * Inserts eventtype into DB (using EventTypeDAO)
	 * 
	 * @param eventtype
	 * @throws ServiceException
	 * @return void
	 * 
	 * 
	 */
	public void insertEventType(EventType eventtype) {
		logger.debug("insertEventType()");
		
		Validator.notNullOrThrow(eventtype, "eventtype is null");		
		Validator.zeroOrThrow(eventtype.getId(),
				"id for new eventtype must be zero - will be autofilled");

		try {
			begin();

			getEventTypeDao().insert(eventtype);
			
			commit();
		} catch (Exception e) {
			rollback();

			throw new ServiceException("Error on inserting data", e);
		} finally {
			closeConnection();
		}
		
		
	}

	/**
	 * Updates eventtype EventType in DB (using EventTypeDAO)
	 * 
	 * @param eventtype
	 * @throws ServiceException
	 * @return void
	 * 
	 * 
	 */
	public void updateEventType(EventType eventtype) {
		logger.debug("updateEventType()");

		try {
			begin();
			
			Validator.notNullOrThrow(eventtype, "eventtype is null");		
			Validator.notZeroOrThrow(eventtype.getId(),
					"id needed for updating eventtype");
			
			getEventTypeDao().update(eventtype);
			
			commit();
		} catch (Exception e) {
			rollback();

			throw new ServiceException("Error on inserting data", e);
		} finally {
			closeConnection();
		}
		
	}

	/**
	 * Deletes eventtype from DB (using EventTypeDAO)
	 * 
	 * @param eventtype
	 * @throws ServiceException
	 * @return void
	 * 
	 * 
	 */
	public void deleteEventType(EventType eventtype) {
		logger.debug("deleteEventType()");

		try {
			begin();
			
			Validator.notNullOrThrow(eventtype, "eventtype is null");		
			Validator.notZeroOrThrow(eventtype.getId(),
					"id needed for deleting eventtype");
			
			getEventTypeDao().delete(eventtype);
			
			commit();
		} catch (Exception e) {
			rollback();

			throw new ServiceException("Error on inserting data", e);
		} finally {
			closeConnection();
		}
		
	}
	
	/**
	 * Searching for eventtypes via id (using EventTypeDAO)
	 * 
	 * @param id
	 * @throws ServiceException
	 * @return EventType
	 * 
	 * 
	 */

	public EventType findById(long id) {
		logger.debug("findById("+id+")");
		
		EventType eventtype= null;
		
		try {
			begin();
			
			if(id == 0){
				throw new ServiceException("id = 0");
			}
			
			eventtype = getEventTypeDao().findById(id);
			commit();
		} catch (Exception e) {
			rollback();

			throw new ServiceException("Error on retrieving data", e);
		} finally {
			closeConnection();
		}
		
		return eventtype;
		
	}

	/**
	 * Searching for all eventtypes (using EventTypeDAO)
	 *
	 * @throws ServiceException
	 * @return List<EventType>
	 * 
	 * 
	 */
	
	public List<EventType> findAll() {
		logger.debug("findAll()");
		List<EventType> eventtype = null;
		try
		{
			begin();
			eventtype = getEventTypeDao().findAll();
			commit();
		} catch (Exception e) {
			rollback();

			throw new ServiceException("Error on retrieving data", e);
		} finally {
			closeConnection();
		} 
		return eventtype;
	}

}
